using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameStateManagement;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Graphics;
using GameStateManagementSample.QuickMath;
namespace GameStateManagementSample.Screens
{
    class Screen_Win : GameScreen
    {
        #region Fields
        ContentManager m_Content;
        InputAction pauseAction;
        Texture2D m_TexBack;
        Rectangle m_RectRetry, m_RectMenu, m_RectNext;
        #endregion

        #region Initialization
        public Screen_Win()
        {
            TransitionOnTime = TimeSpan.FromSeconds(0.5f);
            TransitionOffTime = TimeSpan.FromSeconds(0.5f);
            pauseAction = new InputAction(
                new Buttons[] { Buttons.Start, Buttons.Back },
                new Keys[] { Keys.Escape },
                true);
            m_RectRetry = new Rectangle(240, 250, 70, 70);
            m_RectMenu = new Rectangle(360, 250, 70, 70);
            m_RectNext = new Rectangle(480, 250, 70, 70);

            if (Level.Cur == Level.Unlock)
            {
                Level.Unlock++;
                if (Level.Unlock > Level.Total)
                {
                    Level.Unlock = Level.Total;
                }
            }
            
        }
        public override void Activate(bool instancePreserved)
        {
            if (!instancePreserved)
            {
                if (m_Content == null)
                {
                    m_Content = new ContentManager(ScreenManager.Game.Services, "Content");
                    ScreenManager.Game.ResetElapsedTime();
                    m_TexBack = m_Content.Load<Texture2D>(@"Image/win");
                }
            }
        }
        public override void Deactivate()
        {
            base.Deactivate();
        }
        public override void Unload()
        {
            base.Unload();
        }
        #endregion
        #region Update and Draw
        protected virtual void OnCancel()
        {

        }
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, false);

            if (!otherScreenHasFocus)
            {

            }
        }
        public override void HandleInput(Microsoft.Xna.Framework.GameTime gameTime, InputState input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            PlayerIndex player;
            if (pauseAction.Evaluate(input, ControllingPlayer, out player))
            {
                foreach (GameScreen screen in ScreenManager.GetScreens())
                {
                    screen.ExitScreen();
                }
                ScreenManager.AddScreen(new Screen_Menu(), null);
            }
            TouchCollection touches = input.TouchState;

            foreach (TouchLocation touch in touches)
            {
                if (touch.State == TouchLocationState.Pressed)
                {
                    if (m_RectRetry.Contains((int)touch.Position.X,(int)touch.Position.Y))
                    {
                        TouchRetry();
                    }
                    if (m_RectMenu.Contains((int)touch.Position.X, (int)touch.Position.Y))
                    {
                        TouchMenu();
                    }
                    if (m_RectNext.Contains((int)touch.Position.X, (int)touch.Position.Y))
                    {
                        TouchNext();
                    }
                }
            }
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            float pos = (TransitionPosition) * -320.0f;
            spriteBatch.Begin();
            spriteBatch.Draw(m_TexBack, Vector2.Zero + new Vector2(0,pos), Color.White*TransitionAlpha);
            spriteBatch.End();
        }
        #endregion
#region Touch Button
        private void TouchRetry()
        {
            foreach (GameScreen screen in ScreenManager.GetScreens())
            {
                screen.ExitScreen();
            }
            ScreenManager.AddScreen(new Screen_QuickMath(Level.Cur), null);
        }
        private void TouchMenu()
        {
            foreach (GameScreen screen in ScreenManager.GetScreens())
            {
                screen.ExitScreen();
            }
            ScreenManager.AddScreen(new Screen_Menu(), null);
        }
        private void TouchNext()
        {
            foreach (GameScreen screen in ScreenManager.GetScreens())
            {
                screen.ExitScreen();
            }
            if (Level.Cur < Level.Total)
            {
                ScreenManager.AddScreen(new Screen_QuickMath(Level.Cur+1), null);
            }
            
        }
#endregion
    }
}
